/**
 * Created by cc on 17/7/17.
 */

/**音乐播放器*/
$(function() {

    //播放列表
    var mp3List = [
        ];
    var timer,timertip;
    var _index = 0;//播放索引
    var playerBox = {
        init:function () {
            this.player = $("#h5audio_media").get(0);
            this.playerBox = $("#player");
            this.current = 0;
            this.playerBtn = $("#player").find(".icon-play");
            this.pauseBtn = $("#player").find(".icon-play-pause");
            this.nextBtn = $("#player").find(".icon-next");
            this.preBtn = $("#player").find(".icon-prev");
            $("#player").find(".noUi-stacking").css({left:0});

            this.bind();
            //this.setSrc(0);
        },
        bind:function () {
            this.playerBtn.on(clickevent,function () {
                playerBox.playerBtn.addClass("hide");
                playerBox.pauseBtn.removeClass("hide");
                playerBox.player.pause();
            });
            this.pauseBtn.on(clickevent,function () {
                if(mp3List.length == 0){
                    if(playerBox.player.readyState != 4){
                        $("#tip-cnetent").html("播放列表为空，请添加");
                    }
                    return;
                }else{
                    $("#tip-cnetent").html("");
                }   
                playerBox.playerBtn.removeClass("hide");
                playerBox.pauseBtn.addClass("hide");
                playerBox.player.play();
                //alert(playerBox.player.duration);
            });
            this.nextBtn.on(clickevent,function () {
                playerBox.nextHanderl();
            });
            this.preBtn.on(clickevent,function () {
                _index--;
                if(_index < 0){
                    _index = mp3List.length - 1;
                }
                playerBox.playerBtn.trigger(clickevent);
                playerBox.setSrc(_index);
                var time = setInterval(function(){
                    if(playerBox.player.duration){
                        playerBox.pauseBtn.trigger(clickevent);
                        clearInterval(time);
                    }
                },200);
            });
            
            this.player.addEventListener("ended ",playerBox.nextHanderl);
            this.player.addEventListener("canplay",playerBox.initData);
            this.player.addEventListener("canplaythrough",function () {
                if(playerBox.player.paused){
                    playerBox.initData();
                }
            });
            this.player.addEventListener("timeupdate", playerBox.getCurTime);

            //打开播放器页面
            $(".j-open-player").on(clickevent,function () {
                $("body").addClass("no-scroll");
                $(".mask").removeClass("hide");
                $("#player").removeClass("hide");
            });
            $(".mask").on(clickevent,function () {
                $("body").removeClass("no-scroll");
                $(".mask").addClass("hide");
                $("#player").addClass("hide");
            });
            $("body").on(clickevent,".j-add-to-player",function () {
                var isadd = $(this).attr("data-add");
                if(!isadd){
                    if($(this).attr("data-mp3") && $(this).attr("data-mp3").indexOf(".mp3") > -1) {
                        var _obj = $(this);
                        var _music = {
                            mp3:_obj.attr("data-mp3"),
                            name:_obj.find(".name").text(),
                            title:_obj.find(".title").text()
                        }
                        mp3List.push(_music);
                        $(".box").addClass("hide");
                        $(this).attr("data-add",1);
                        playerBox.tipHandler("成功加入播放列表");
                    }
                    playerBox.setSrc(mp3List.length - 1);
                    //添加到播放列表
                    playerBox.addList();
                }else{
                    playerBox.tipHandler("该歌曲已经存在播放列表");
                }
            });
            //切换play
            $("body").on(clickevent,"li.line1",function () {
                var index = $(this).data("index");
                if($(this).hasClass("play")){
                    return;
                }
                playerBox.setSrc(index);
            });
            //连续播放
            $("body").on(clickevent,".j-lianxu-btn",function () {
                $(".j-add-to-player").each(function () {
                    var isadd = $(this).attr("data-add");
                    if(!isadd && $(this).attr("data-mp3")){
                        var _obj = $(this);
                        var _music = {
                            mp3:_obj.attr("data-mp3").trim(),
                            name:_obj.find(".name").text(),
                            title:_obj.find(".title").text()
                        }
                        mp3List.push(_music);
                        //mp3List.push($(this).attr("data-mp3"));
                        $(this).attr("data-add",1);
                    }
                });
                if(mp3List.length > 0){
                    playerBox.setSrc(0);
                    playerBox.tipHandler("成功加入播放列表");
                }
                //添加到播放列表
                playerBox.addList();
            });
        },
        tipHandler:function(msg){
            if(timertip){
                clearTimeout(timertip);
            }
            $(".j-tips").remove();
            $("body").append("<div class='j-tips t-show tip-content'><span>"+msg+"</span></div>");
            timertip = setTimeout(function(){
                $(".j-tips").remove();
            },2000);
        },
        addList:function () {
            var html = "";
            for (var i=0; i < mp3List.length ; i++){
                if(_index == i){
                    html =  html + '<li class="line1 play" data-index="'+i+'"><i class="iconfont">&#xe607;</i>'+mp3List[i].title+'<span>'+mp3List[i].name+'</span></li>';
                }else{
                    html =  html + '<li class="line1" data-index="'+i+'"><i class="iconfont">&#xe600;</i>'+mp3List[i].title+'<span>'+mp3List[i].name+'</span></li>';
                }
            }
            $("#player ul").html(html);
        },
        setTime:function () {
            clearInterval(timer);
            timer = setInterval(function(){
                if(!playerBox.player.paused){
                    playerBox.playerBtn.removeClass("hide");
                    playerBox.pauseBtn.addClass("hide");
                }
                if(playerBox.player.readyState != 4){
                    $("#tip-cnetent").html("加载中");
                }else{
                    $("#tip-cnetent").html("");
                }
            },500);
        },

        nextHanderl:function () {
            _index++;
            if(_index == mp3List.length){
                _index = 0;
            }
            playerBox.playerBtn.trigger(clickevent);
            playerBox.setSrc(_index);
            var time = setInterval(function(){
                if(playerBox.player.duration){
                    clearInterval(time);
                    playerBox.pauseBtn.trigger(clickevent);
                }
            },100);
        },
        setSrc:function (index) {
            $("#player ul li").removeClass("play").find("i").html("&#xe600;");
            $("#player ul li").eq(index).addClass("play").find("i").html("&#xe607;");

            _index = index;
            this.player.src = mp3List[index].mp3;
            this.player.load();
            playerBox.setTime();
        },
        initData:function () {
            //alert(playerBox.player.duration);
            $("#player").find(".j-duration").html(playerBox.toSenconds(playerBox.player.duration));
            $("#player").find(".ticker").html("0:00");
            $("#player").find(".noUi-stacking").css({left:0});
            playerBox.player.play();
        },
        //转成分钟
        toSenconds:function(time){
            var _m = (time / 60).toFixed(0) ,
                _s = Math.ceil(time % 60);
            _s = _s > 10 ? _s : "0"+_s;
            return _m+":"+_s;
        },
        getCurTime:function () {
            $("#player").find(".ticker").html(playerBox.toSenconds(playerBox.player.currentTime));
            $("#player").find(".noUi-stacking").css({
                left:playerBox.player.currentTime / playerBox.player.duration * 100 + "%"
            });
        }
    }
    playerBox.init();
});